package com.example.demo.messageWall.controller;

import com.example.demo.messageWall.model.MessageInfo;
import com.example.demo.messageWall.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    public MessageService messageService;
    //private List<MessageInfo> list = new ArrayList<>();
    @RequestMapping("/publish")
    public Boolean publishMessage(MessageInfo messageInfo){
        if(!StringUtils.hasLength(messageInfo.getFrom()) ||
                !StringUtils.hasLength(messageInfo.getTo()) ||
                !StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }
        Integer res = messageService.insertMessage(messageInfo);
        if(res < 1){
            return false;
        }
        //list.add(messageInfo);
        return true;
    }
    @RequestMapping("/messageInfo")
    public List<MessageInfo> getMessageInfo(){
        //return list;
        return messageService.getAllList();
    }
}
